#pragma once
#include <assert.h>

namespace My_vector
{
	template <class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		// constructor
		vector()
			:_start(nullptr)
			, _finish(nullptr)
			, _endofstorge(nullptr)
		{}

		template <class InputIterator>
		vector(InputIterator first, InputIterator last)
			: _start(nullptr)
			, _finish(nullptr)
			, _endofstorge(nullptr)
		{

			while (first != last)
			{
				push_back(*first);
				first++;
			}
		}

		vector(size_t n, const T& val = T())
		{
			reserve(n);
			for (int i = 0; i < n; i++)
			{
				push_back(val);
			}
		}

		vector(int n, const T& val = T())
		{
			reserve(n);
			for (int i = 0; i < n; i++)
			{
				push_back(val);
			}
		}

		vector(vector<T>& v)
		{
			reserve(v.capacity());
			for (auto& e : v)
			{
				push_back(e);
			}
		}

		// destructor
		~vector()
		{
			delete[] _start;
			_start = _finish = _endofstorge = nullptr;
		}

		// operator=
		vector<T>& operator=(vector<T>& v)
		{
			//_start = new T[v.capacity()];
			//_finish = _start + v.size();
			//_endofstorge = _start + v.capacity();
			swap(v);
			return *this;
		}

		// capacity
		size_t size()
		{
			return _finish - _start;
		}
		void resize(size_t num, const T& val = T())
		{
			if (num < size())
			{
				_finish = _start + num;
			}
			else
			{
				while (_finish != _start + num)
				{
					push_back(val);
					_finish++;
				}
			}
		}
		size_t capacity()
		{
			return _endofstorge - _start;
		}
		void reserve(size_t num)
		{
			if (num > capacity())
			{
				T* tmp = new T[num];
				size_t sz = size();
				if (_start)
				{
					for (size_t i = 0; i < sz; i++)
					{
						tmp[i] = _start[i];
					}

					delete[] _start;
				}

				_start = tmp;
				_finish = _start + sz;
				_endofstorge = _start + num;
			}
		}
		bool empty()
		{
			if (_start == _finish)
				return true;
			return false;
		}

		// element access
		T& operator[](size_t pos)
		{
			return *(_start + pos);
		}

		// iterator
		iterator begin()
		{
			return _start;
		}
		iterator end()
		{
			return _finish;
		}
		const_iterator begin() const
		{
			return _start;
		}
		const_iterator end() const
		{
			return _finish;
		}

		// modifier
		void push_back(T data)
		{
			if (_finish == _endofstorge)
			{
				size_t sz = size();
				size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
				T* tmp = new T[newcapacity];

				if (_start)
				{
					//memcpy(tmp, _start, sizeof(T) * size());
					for (size_t i = 0; i < sz; i++)
					{
						tmp[i] = _start[i];
					}
					delete[] _start;
				}

				_finish = tmp + sz;
				_endofstorge = tmp + newcapacity;
				_start = tmp;
			}

			assert(_finish);
			*(_finish) = data;
			++_finish;
		}

		void pop_back()
		{
			--_finish;
		}

		void insert(iterator pos, const T& x)
		{
			assert(pos >= _start);
			assert(pos <= _finish);

			if (_finish == _endofstorge)
			{
				size_t len = pos - _start;
				reserve(capacity() == 0 ? 4 : capacity() * 2);
				pos = _start + len;
			}

			iterator end = _finish - 1;
			while (end >= pos)
			{
				*(end + 1) = *end;
				--end;
			}

			*pos = x;
			++_finish;
		}

		iterator erase(iterator pos)
		{
			assert(pos >= _start);
			assert(pos < _finish);

			iterator it = pos + 1;
			while (it < _finish)
			{
				*(it - 1) = *it;
				++it;
			}

			--_finish;

			return pos;
		}


		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_endofstorge, v._endofstorge);
		}

	private:
		iterator _start;
		iterator _finish;
		iterator _endofstorge;
	};

	//void test_vector1()
	//{
	//	vector<int> v;
	//	v.push_back(1);
	//	v.push_back(2);
	//	v.push_back(3);
	//	v.push_back(4);
	//	v.push_back(5);
	//	v.push_back(6);
	//	v.push_back(7);
	//	for (auto e : v)
	//	{
	//		cout << e << " ";
	//	}
	//	cout << endl;

	//	v.pop_back();
	//	v.pop_back();
	//	v.pop_back();
	//	for (auto e : v)
	//	{
	//		cout << e << " ";
	//	}
	//	cout << endl;

	//	v.insert(0, 0);
	//	for (auto e : v)
	//	{
	//		cout << e << " ";
	//	}	
	//	cout << endl;

	//	v.erase(0);
	//	for (auto e : v)
	//	{
	//		cout << e << " ";
	//	}
	//	cout << endl;
	//}

	//void test_vector1()
	//{
	//	vector<int> v(5,5);

	//	auto it = v.begin();

	//	v.insert(it, 10);
	//	v.insert(it, 10);
	//	v.insert(it, 10);
	//	v.insert(it, 10);
	//	v.insert(it, 10);

	//	for (auto e : v)
	//	{
	//		cout << e << " ";
	//	}
	//	cout << endl;
	//}


	void test_vector1()
	{
		vector<string> v;
		v.push_back("111111111111111111111");
		v.push_back("111111111111111111111");
		v.push_back("111111111111111111111");
		v.push_back("111111111111111111111");
		v.push_back("111111111111111111111");


		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;
	}
}




